package com.yjhb.android.sbz.activity;

import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.yjhb.android.sbz.R;

import com.yjhb.android.sbz.dialog.TipsDialog;
import com.yjhb.android.sbz.manager.SysManager;
import com.yjhb.android.sbz.tool.AppUpdateTool;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.base.mvp.BasePresenter;
import com.jojo.android.zxlib.interfaces.Callback;
import com.jojo.android.zxlib.tool.ZxActivityTool;
import com.jojo.android.zxlib.tool.ZxAppTool;
import com.jojo.android.zxlib.tool.ZxDataTool;
import com.jojo.android.zxlib.tool.ZxFileTool;
import com.jojo.android.zxlib.tool.ZxLogTool;
import com.jojo.android.zxlib.tool.ZxToast;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2020/11/19 15:36
 */
public class SettingActivity extends BaseActivity {
    private final String TAG = "SettingActivity";

    @BindView(R.id.version_tv)
    TextView version_tv;
    @BindView(R.id.clearCache_tv)
    TextView clearCache_tv;
    @BindView(R.id.clearData_tv)
    TextView clearData_tv;
    @BindView(R.id.clearData)
    ViewGroup clearData;

    @Override
    protected BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected boolean isImmerse() {
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initUI() {
        version_tv.setText(ZxAppTool.getAppVersionName(this));
        clearCache_tv.setText(ZxDataTool.byte2FitSize(ZxFileTool.getFileAllSize(getCacheDir().getAbsolutePath())));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            clearData_tv.setText(ZxDataTool.byte2FitSize(ZxFileTool.getFileAllSize(getDataDir().getAbsolutePath())));
        } else {
            clearCache_tv.setText("");
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void regEvent() {

    }

    public void onBack(View view) {
        finish();
    }

    @OnClick(R.id.rl_exit)
    public void rl_exit() {
        SysManager.exitLogin(this);
    }

    @OnClick(R.id.about)
    public void about() {
        ZxActivityTool.skipActivity(this, AboutActivity.class);
    }

    @OnClick(R.id.clearCache)
    public void clearCache() {
        boolean b = ZxFileTool.deleteFilesInDir(getCacheDir());
        if (b) {
            ZxLogTool.i("缓存已清除");
            ZxToast.showSuccessBottomToast("缓存已清除");
        }
        initUI();
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @OnClick(R.id.clearData)
    public void clearData() {
        boolean b = ZxFileTool.deleteFilesInDir(getDataDir());
        if (b) {
            ZxLogTool.i("数据已清除");
            TipsDialog tipsDialog = new TipsDialog(this);
            tipsDialog.setTitle("提示");
            tipsDialog.setButtonVisibility(TipsDialog.TYPE_CANCEL, View.GONE);
            tipsDialog.setContent("数据已清除完毕，需重新启动才能生效！");
            tipsDialog.setOnClickListener(type -> {
                tipsDialog.dismiss();
                ZxActivityTool.finishAllActivity();
                System.exit(0);
            });
            tipsDialog.setCancelable(false);
            tipsDialog.setCanceledOnTouchOutside(false);
            tipsDialog.show();
        }
        initUI();
    }

    @OnClick(R.id.privacy)
    public void privacy() {
        Tool.startWebActivity(this, "http://www.shangbangzuan.com:81/privacy_policy.html", "隐私政策");
    }

    @OnClick(R.id.userAgreement)
    public void userAgreement() {
        Tool.startWebActivity(this, "http://www.shangbangzuan.com:81/user_agreement.html", "用户协议");
    }

    @OnClick(R.id.update)
    public void update() {
        AppUpdateTool appUpdateTool = new AppUpdateTool(this);
        appUpdateTool.setCallback(new Callback<Boolean>() {
            @Override
            public void callback(Boolean aBoolean) {
                if (!aBoolean) {
                    ZxToast.showINFOBottomToast("已是最新版本。");
                }
            }
        });
        appUpdateTool.check();
    }

}
